<!DOCTYPE html>
<html lang="en-US">
  
<!-- Mirrored from heavy_code_industry.gitee.io/code_heavy_industry/pro015-Distributed-Transaction/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 10 May 2022 04:34:16 GMT -->
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>分布式事务 | 代码重工</title>
    <meta name="generator" content="VuePress 1.9.5">
    
    <meta name="description" content="代码重工">
    
    <link rel="preload" href="../assets/css/0.styles.f7de6701.css" as="style"><link rel="preload" href="../assets/js/app.972bd0f0.js" as="script"><link rel="preload" href="../assets/js/4.904e76b5.js" as="script"><link rel="preload" href="../assets/js/8.e2571ee7.js" as="script"><link rel="prefetch" href="../assets/js/1.d66e04b2.js"><link rel="prefetch" href="../assets/js/10.7093d550.js"><link rel="prefetch" href="../assets/js/100.384331b2.js"><link rel="prefetch" href="../assets/js/101.582f78e7.js"><link rel="prefetch" href="../assets/js/102.17f2c0ec.js"><link rel="prefetch" href="../assets/js/103.a530857d.js"><link rel="prefetch" href="../assets/js/104.62821ce1.js"><link rel="prefetch" href="../assets/js/105.11cdcc82.js"><link rel="prefetch" href="../assets/js/106.d57c1e75.js"><link rel="prefetch" href="../assets/js/107.7ebeb306.js"><link rel="prefetch" href="../assets/js/108.c067b47b.js"><link rel="prefetch" href="../assets/js/109.9495d2ca.js"><link rel="prefetch" href="../assets/js/11.acc2935e.js"><link rel="prefetch" href="../assets/js/110.7f58dbd3.js"><link rel="prefetch" href="../assets/js/111.f78239ff.js"><link rel="prefetch" href="../assets/js/112.8eb72445.js"><link rel="prefetch" href="../assets/js/113.3f5aff71.js"><link rel="prefetch" href="../assets/js/114.9f65cf57.js"><link rel="prefetch" href="../assets/js/115.dcaea004.js"><link rel="prefetch" href="../assets/js/116.726c5aac.js"><link rel="prefetch" href="../assets/js/117.54d6c26a.js"><link rel="prefetch" href="../assets/js/118.3f4e0bda.js"><link rel="prefetch" href="../assets/js/119.95ed5dc6.js"><link rel="prefetch" href="../assets/js/12.a5354f50.js"><link rel="prefetch" href="../assets/js/120.4fd08fac.js"><link rel="prefetch" href="../assets/js/121.f4aadbe5.js"><link rel="prefetch" href="../assets/js/122.d588a1e0.js"><link rel="prefetch" href="../assets/js/123.781e7192.js"><link rel="prefetch" href="../assets/js/124.ed312291.js"><link rel="prefetch" href="../assets/js/125.bceffbb9.js"><link rel="prefetch" href="../assets/js/126.51bf552f.js"><link rel="prefetch" href="../assets/js/127.4800687b.js"><link rel="prefetch" href="../assets/js/128.fcdd6f2a.js"><link rel="prefetch" href="../assets/js/129.2a25d78a.js"><link rel="prefetch" href="../assets/js/13.88a0c565.js"><link rel="prefetch" href="../assets/js/130.dcfe7623.js"><link rel="prefetch" href="../assets/js/131.c29e7c45.js"><link rel="prefetch" href="../assets/js/132.9a412a4b.js"><link rel="prefetch" href="../assets/js/133.615218d5.js"><link rel="prefetch" href="../assets/js/134.2168ca5a.js"><link rel="prefetch" href="../assets/js/135.a5e262ff.js"><link rel="prefetch" href="../assets/js/136.7dba5235.js"><link rel="prefetch" href="../assets/js/137.445576e7.js"><link rel="prefetch" href="../assets/js/138.577b3f66.js"><link rel="prefetch" href="../assets/js/139.b646f1ac.js"><link rel="prefetch" href="../assets/js/14.36391765.js"><link rel="prefetch" href="../assets/js/140.52abf81e.js"><link rel="prefetch" href="../assets/js/141.807c626b.js"><link rel="prefetch" href="../assets/js/142.adf9e127.js"><link rel="prefetch" href="../assets/js/143.9773d979.js"><link rel="prefetch" href="../assets/js/144.5658ae76.js"><link rel="prefetch" href="../assets/js/145.2d2fb13b.js"><link rel="prefetch" href="../assets/js/146.0e322019.js"><link rel="prefetch" href="../assets/js/147.50b34ac1.js"><link rel="prefetch" href="../assets/js/148.463aab7c.js"><link rel="prefetch" href="../assets/js/149.9bd7c539.js"><link rel="prefetch" href="../assets/js/15.4a8e9d28.js"><link rel="prefetch" href="../assets/js/150.9449ca28.js"><link rel="prefetch" href="../assets/js/151.1961c6db.js"><link rel="prefetch" href="../assets/js/152.18709656.js"><link rel="prefetch" href="../assets/js/153.a4bf78f6.js"><link rel="prefetch" href="../assets/js/154.6c90b1d5.js"><link rel="prefetch" href="../assets/js/155.024247e2.js"><link rel="prefetch" href="../assets/js/156.76281b57.js"><link rel="prefetch" href="../assets/js/157.a7c02212.js"><link rel="prefetch" href="../assets/js/158.0d00acef.js"><link rel="prefetch" href="../assets/js/159.9180c937.js"><link rel="prefetch" href="../assets/js/16.87793b5d.js"><link rel="prefetch" href="../assets/js/160.d7a98288.js"><link rel="prefetch" href="../assets/js/161.389ddf84.js"><link rel="prefetch" href="../assets/js/162.ab22d7dc.js"><link rel="prefetch" href="../assets/js/163.087257c6.js"><link rel="prefetch" href="../assets/js/164.addfc5af.js"><link rel="prefetch" href="../assets/js/165.f90e6b2a.js"><link rel="prefetch" href="../assets/js/166.fa06e4b6.js"><link rel="prefetch" href="../assets/js/167.a00e722b.js"><link rel="prefetch" href="../assets/js/168.e3855223.js"><link rel="prefetch" href="../assets/js/169.e0c63a4e.js"><link rel="prefetch" href="../assets/js/17.dec86310.js"><link rel="prefetch" href="../assets/js/170.7c5835f4.js"><link rel="prefetch" href="../assets/js/171.7d9ae110.js"><link rel="prefetch" href="../assets/js/172.4da5caea.js"><link rel="prefetch" href="../assets/js/173.f8ac6155.js"><link rel="prefetch" href="../assets/js/174.3b831765.js"><link rel="prefetch" href="../assets/js/175.01782f3d.js"><link rel="prefetch" href="../assets/js/176.6eab6644.js"><link rel="prefetch" href="../assets/js/177.86339767.js"><link rel="prefetch" href="../assets/js/178.c437c804.js"><link rel="prefetch" href="../assets/js/179.927227c9.js"><link rel="prefetch" href="../assets/js/18.9167733d.js"><link rel="prefetch" href="../assets/js/180.caa2c660.js"><link rel="prefetch" href="../assets/js/181.c186b8ac.js"><link rel="prefetch" href="../assets/js/182.0475844f.js"><link rel="prefetch" href="../assets/js/183.2c5950c8.js"><link rel="prefetch" href="../assets/js/184.82b336f6.js"><link rel="prefetch" href="../assets/js/185.e2d4a46e.js"><link rel="prefetch" href="../assets/js/186.99a3de8a.js"><link rel="prefetch" href="../assets/js/187.d557500c.js"><link rel="prefetch" href="../assets/js/188.e81bd251.js"><link rel="prefetch" href="../assets/js/189.d07d368a.js"><link rel="prefetch" href="../assets/js/19.017b4ad0.js"><link rel="prefetch" href="../assets/js/190.173606ea.js"><link rel="prefetch" href="../assets/js/191.c5ac0c71.js"><link rel="prefetch" href="../assets/js/192.127d6d93.js"><link rel="prefetch" href="../assets/js/193.2f9011c9.js"><link rel="prefetch" href="../assets/js/194.14f534a3.js"><link rel="prefetch" href="../assets/js/195.65939036.js"><link rel="prefetch" href="../assets/js/196.6e3cf025.js"><link rel="prefetch" href="../assets/js/197.4d18b1d7.js"><link rel="prefetch" href="../assets/js/198.c2e521c3.js"><link rel="prefetch" href="../assets/js/199.d72c63dd.js"><link rel="prefetch" href="../assets/js/20.6aa686d0.js"><link rel="prefetch" href="../assets/js/200.56d7dbd3.js"><link rel="prefetch" href="../assets/js/201.c72a395d.js"><link rel="prefetch" href="../assets/js/202.cb32f7af.js"><link rel="prefetch" href="../assets/js/203.285e6b92.js"><link rel="prefetch" href="../assets/js/204.ead50027.js"><link rel="prefetch" href="../assets/js/205.190c1baf.js"><link rel="prefetch" href="../assets/js/206.4dab4ba0.js"><link rel="prefetch" href="../assets/js/207.f11eee48.js"><link rel="prefetch" href="../assets/js/208.e2f8ca82.js"><link rel="prefetch" href="../assets/js/209.e7e74aff.js"><link rel="prefetch" href="../assets/js/21.f7d66fe8.js"><link rel="prefetch" href="../assets/js/210.69c4a0ed.js"><link rel="prefetch" href="../assets/js/211.a1ab5062.js"><link rel="prefetch" href="../assets/js/212.6d0473bd.js"><link rel="prefetch" href="../assets/js/213.bd120419.js"><link rel="prefetch" href="../assets/js/214.c7d4362c.js"><link rel="prefetch" href="../assets/js/215.8adc34dc.js"><link rel="prefetch" href="../assets/js/216.abf8ff23.js"><link rel="prefetch" href="../assets/js/217.10f9fc46.js"><link rel="prefetch" href="../assets/js/218.52e31fec.js"><link rel="prefetch" href="../assets/js/219.ea0551af.js"><link rel="prefetch" href="../assets/js/22.b1de9b18.js"><link rel="prefetch" href="../assets/js/220.6be520c8.js"><link rel="prefetch" href="../assets/js/221.c0d93576.js"><link rel="prefetch" href="../assets/js/222.1bb0eaff.js"><link rel="prefetch" href="../assets/js/223.8492b065.js"><link rel="prefetch" href="../assets/js/224.5897434e.js"><link rel="prefetch" href="../assets/js/225.51902428.js"><link rel="prefetch" href="../assets/js/226.9da70eee.js"><link rel="prefetch" href="../assets/js/227.9e113839.js"><link rel="prefetch" href="../assets/js/228.d689b27d.js"><link rel="prefetch" href="../assets/js/229.28f74090.js"><link rel="prefetch" href="../assets/js/23.e3b58d3d.js"><link rel="prefetch" href="../assets/js/230.465a35a0.js"><link rel="prefetch" href="../assets/js/231.219aeb63.js"><link rel="prefetch" href="../assets/js/232.84035c50.js"><link rel="prefetch" href="../assets/js/233.ef27424d.js"><link rel="prefetch" href="../assets/js/234.5dcf1349.js"><link rel="prefetch" href="../assets/js/235.bcc0c537.js"><link rel="prefetch" href="../assets/js/236.9bb64465.js"><link rel="prefetch" href="../assets/js/237.f3300514.js"><link rel="prefetch" href="../assets/js/238.ca7af9ac.js"><link rel="prefetch" href="../assets/js/239.fdfe2127.js"><link rel="prefetch" href="../assets/js/24.2e010582.js"><link rel="prefetch" href="../assets/js/240.8a4488af.js"><link rel="prefetch" href="../assets/js/241.9987dfab.js"><link rel="prefetch" href="../assets/js/242.819ed984.js"><link rel="prefetch" href="../assets/js/243.bf129ecd.js"><link rel="prefetch" href="../assets/js/244.030b4b49.js"><link rel="prefetch" href="../assets/js/245.7663f620.js"><link rel="prefetch" href="../assets/js/246.ed2b2fc0.js"><link rel="prefetch" href="../assets/js/247.54dd84f1.js"><link rel="prefetch" href="../assets/js/248.a1c69038.js"><link rel="prefetch" href="../assets/js/249.47dd7b3b.js"><link rel="prefetch" href="../assets/js/25.a5f2f737.js"><link rel="prefetch" href="../assets/js/250.ec8017a5.js"><link rel="prefetch" href="../assets/js/251.89ab0cf7.js"><link rel="prefetch" href="../assets/js/252.5ec1eea4.js"><link rel="prefetch" href="../assets/js/253.31a37280.js"><link rel="prefetch" href="../assets/js/254.fcfabb55.js"><link rel="prefetch" href="../assets/js/255.286f4c17.js"><link rel="prefetch" href="../assets/js/256.ce77595b.js"><link rel="prefetch" href="../assets/js/257.cb3677c0.js"><link rel="prefetch" href="../assets/js/258.ca18e0e9.js"><link rel="prefetch" href="../assets/js/259.65dc8492.js"><link rel="prefetch" href="../assets/js/26.cce346fc.js"><link rel="prefetch" href="../assets/js/260.475ddaba.js"><link rel="prefetch" href="../assets/js/261.643b784a.js"><link rel="prefetch" href="../assets/js/262.e5092c67.js"><link rel="prefetch" href="../assets/js/263.5e85d32e.js"><link rel="prefetch" href="../assets/js/264.6ff4b789.js"><link rel="prefetch" href="../assets/js/265.fee7416a.js"><link rel="prefetch" href="../assets/js/266.8d8a5e44.js"><link rel="prefetch" href="../assets/js/267.8c62a655.js"><link rel="prefetch" href="../assets/js/268.762073ff.js"><link rel="prefetch" href="../assets/js/269.ffe69921.js"><link rel="prefetch" href="../assets/js/27.22790cda.js"><link rel="prefetch" href="../assets/js/270.d5bf783f.js"><link rel="prefetch" href="../assets/js/271.83fd2f70.js"><link rel="prefetch" href="../assets/js/272.6de67fa9.js"><link rel="prefetch" href="../assets/js/273.628e1b77.js"><link rel="prefetch" href="../assets/js/274.7f0d82c6.js"><link rel="prefetch" href="../assets/js/275.d4478098.js"><link rel="prefetch" href="../assets/js/276.56649e5b.js"><link rel="prefetch" href="../assets/js/277.05b881f2.js"><link rel="prefetch" href="../assets/js/278.c079de28.js"><link rel="prefetch" href="../assets/js/279.58101972.js"><link rel="prefetch" href="../assets/js/28.ac0ba3fa.js"><link rel="prefetch" href="../assets/js/280.5e32c770.js"><link rel="prefetch" href="../assets/js/281.59fb6cf4.js"><link rel="prefetch" href="../assets/js/282.f22da512.js"><link rel="prefetch" href="../assets/js/283.db103d63.js"><link rel="prefetch" href="../assets/js/284.a373c7e0.js"><link rel="prefetch" href="../assets/js/285.55c6f720.js"><link rel="prefetch" href="../assets/js/286.f924c38c.js"><link rel="prefetch" href="../assets/js/287.52a35525.js"><link rel="prefetch" href="../assets/js/288.dcc32270.js"><link rel="prefetch" href="../assets/js/289.835ad2b5.js"><link rel="prefetch" href="../assets/js/29.6e1dc238.js"><link rel="prefetch" href="../assets/js/290.749e38a9.js"><link rel="prefetch" href="../assets/js/291.90916822.js"><link rel="prefetch" href="../assets/js/292.b453b275.js"><link rel="prefetch" href="../assets/js/293.11ad0754.js"><link rel="prefetch" href="../assets/js/294.dddc45b9.js"><link rel="prefetch" href="../assets/js/295.74ecca38.js"><link rel="prefetch" href="../assets/js/296.2061efa6.js"><link rel="prefetch" href="../assets/js/297.b838fcd5.js"><link rel="prefetch" href="../assets/js/298.8c08be1f.js"><link rel="prefetch" href="../assets/js/299.5a99490b.js"><link rel="prefetch" href="../assets/js/3.9ca9fb75.js"><link rel="prefetch" href="../assets/js/30.64f9cad0.js"><link rel="prefetch" href="../assets/js/300.a5010762.js"><link rel="prefetch" href="../assets/js/301.a2f8795a.js"><link rel="prefetch" href="../assets/js/302.129bdff3.js"><link rel="prefetch" href="../assets/js/303.57268b2c.js"><link rel="prefetch" href="../assets/js/304.5556b94f.js"><link rel="prefetch" href="../assets/js/305.72bbf9b8.js"><link rel="prefetch" href="../assets/js/306.821163a7.js"><link rel="prefetch" href="../assets/js/307.b85ceac7.js"><link rel="prefetch" href="../assets/js/308.7f794216.js"><link rel="prefetch" href="../assets/js/309.212b7a1d.js"><link rel="prefetch" href="../assets/js/31.386297cc.js"><link rel="prefetch" href="../assets/js/310.089b4a08.js"><link rel="prefetch" href="../assets/js/311.c7f6aabd.js"><link rel="prefetch" href="../assets/js/312.810271c0.js"><link rel="prefetch" href="../assets/js/313.030424f0.js"><link rel="prefetch" href="../assets/js/314.a95451b6.js"><link rel="prefetch" href="../assets/js/315.f883b88e.js"><link rel="prefetch" href="../assets/js/316.676ed945.js"><link rel="prefetch" href="../assets/js/317.0dcbeaef.js"><link rel="prefetch" href="../assets/js/318.bd172af6.js"><link rel="prefetch" href="../assets/js/319.e2d01ca1.js"><link rel="prefetch" href="../assets/js/32.8927ba28.js"><link rel="prefetch" href="../assets/js/320.09b01482.js"><link rel="prefetch" href="../assets/js/321.3b70dce2.js"><link rel="prefetch" href="../assets/js/322.5f7046ad.js"><link rel="prefetch" href="../assets/js/323.ffd56783.js"><link rel="prefetch" href="../assets/js/324.cfe0b188.js"><link rel="prefetch" href="../assets/js/325.cc2f1112.js"><link rel="prefetch" href="../assets/js/326.635a861b.js"><link rel="prefetch" href="../assets/js/327.94eacc0e.js"><link rel="prefetch" href="../assets/js/328.7d4790fb.js"><link rel="prefetch" href="../assets/js/329.38f7ef56.js"><link rel="prefetch" href="../assets/js/33.bd1f28e6.js"><link rel="prefetch" href="../assets/js/330.269edfe2.js"><link rel="prefetch" href="../assets/js/331.21623a36.js"><link rel="prefetch" href="../assets/js/332.1f1c659c.js"><link rel="prefetch" href="../assets/js/333.fe34384d.js"><link rel="prefetch" href="../assets/js/334.34a514a7.js"><link rel="prefetch" href="../assets/js/335.9272b0cc.js"><link rel="prefetch" href="../assets/js/336.796dcaf9.js"><link rel="prefetch" href="../assets/js/337.bfd29430.js"><link rel="prefetch" href="../assets/js/338.fdb020fd.js"><link rel="prefetch" href="../assets/js/339.d7f19890.js"><link rel="prefetch" href="../assets/js/34.36a2460a.js"><link rel="prefetch" href="../assets/js/340.b22b3f52.js"><link rel="prefetch" href="../assets/js/341.47e1d3f4.js"><link rel="prefetch" href="../assets/js/342.38ca17d1.js"><link rel="prefetch" href="../assets/js/343.2e7874e5.js"><link rel="prefetch" href="../assets/js/344.6697aea1.js"><link rel="prefetch" href="../assets/js/345.ad3be23a.js"><link rel="prefetch" href="../assets/js/346.d40fa749.js"><link rel="prefetch" href="../assets/js/347.881934aa.js"><link rel="prefetch" href="../assets/js/348.2e8d29b9.js"><link rel="prefetch" href="../assets/js/349.87f51cb4.js"><link rel="prefetch" href="../assets/js/35.80f36815.js"><link rel="prefetch" href="../assets/js/350.06081b68.js"><link rel="prefetch" href="../assets/js/351.f883c9ae.js"><link rel="prefetch" href="../assets/js/352.8fcd0728.js"><link rel="prefetch" href="../assets/js/353.2dd787b0.js"><link rel="prefetch" href="../assets/js/354.689db664.js"><link rel="prefetch" href="../assets/js/355.b9a083ea.js"><link rel="prefetch" href="../assets/js/356.3eaa4bf3.js"><link rel="prefetch" href="../assets/js/357.2f05ac11.js"><link rel="prefetch" href="../assets/js/358.c6777610.js"><link rel="prefetch" href="../assets/js/359.2ae0c8ac.js"><link rel="prefetch" href="../assets/js/36.f9bba134.js"><link rel="prefetch" href="../assets/js/360.c9811603.js"><link rel="prefetch" href="../assets/js/361.5c3068d4.js"><link rel="prefetch" href="../assets/js/362.a80ddcc6.js"><link rel="prefetch" href="../assets/js/363.ccbb8d6e.js"><link rel="prefetch" href="../assets/js/364.8da5de40.js"><link rel="prefetch" href="../assets/js/365.0d9f6c8a.js"><link rel="prefetch" href="../assets/js/366.e410e582.js"><link rel="prefetch" href="../assets/js/367.f8689b17.js"><link rel="prefetch" href="../assets/js/368.57119763.js"><link rel="prefetch" href="../assets/js/369.f92c7aed.js"><link rel="prefetch" href="../assets/js/37.8935c848.js"><link rel="prefetch" href="../assets/js/370.8e896c3a.js"><link rel="prefetch" href="../assets/js/371.e2565759.js"><link rel="prefetch" href="../assets/js/372.0028b0ce.js"><link rel="prefetch" href="../assets/js/373.82bfe997.js"><link rel="prefetch" href="../assets/js/374.434f814f.js"><link rel="prefetch" href="../assets/js/375.8478c681.js"><link rel="prefetch" href="../assets/js/376.998ce2de.js"><link rel="prefetch" href="../assets/js/377.670e9be5.js"><link rel="prefetch" href="../assets/js/378.c9cfc01b.js"><link rel="prefetch" href="../assets/js/379.0aacc719.js"><link rel="prefetch" href="../assets/js/38.86fba1a7.js"><link rel="prefetch" href="../assets/js/380.14a91411.js"><link rel="prefetch" href="../assets/js/381.9e783c93.js"><link rel="prefetch" href="../assets/js/382.cf2ffc03.js"><link rel="prefetch" href="../assets/js/383.c32b0740.js"><link rel="prefetch" href="../assets/js/384.4280025f.js"><link rel="prefetch" href="../assets/js/385.a816d541.js"><link rel="prefetch" href="../assets/js/386.1d8fc395.js"><link rel="prefetch" href="../assets/js/387.bc22626f.js"><link rel="prefetch" href="../assets/js/388.1cf59352.js"><link rel="prefetch" href="../assets/js/389.a09795bf.js"><link rel="prefetch" href="../assets/js/39.c8c0ff8e.js"><link rel="prefetch" href="../assets/js/390.6a223692.js"><link rel="prefetch" href="../assets/js/391.733d45f0.js"><link rel="prefetch" href="../assets/js/392.d76fd3df.js"><link rel="prefetch" href="../assets/js/393.c885df90.js"><link rel="prefetch" href="../assets/js/394.c344d389.js"><link rel="prefetch" href="../assets/js/395.687cbd0a.js"><link rel="prefetch" href="../assets/js/396.57e0df54.js"><link rel="prefetch" href="../assets/js/397.1a31ad36.js"><link rel="prefetch" href="../assets/js/398.59535325.js"><link rel="prefetch" href="../assets/js/399.0a11557f.js"><link rel="prefetch" href="../assets/js/40.c0f7e504.js"><link rel="prefetch" href="../assets/js/400.7b2afabb.js"><link rel="prefetch" href="../assets/js/401.4b72ea6e.js"><link rel="prefetch" href="../assets/js/402.b94737bf.js"><link rel="prefetch" href="../assets/js/403.a838c189.js"><link rel="prefetch" href="../assets/js/404.9a221abc.js"><link rel="prefetch" href="../assets/js/405.f8f3b954.js"><link rel="prefetch" href="../assets/js/406.2de8752c.js"><link rel="prefetch" href="../assets/js/407.eb35a188.js"><link rel="prefetch" href="../assets/js/408.47acaedf.js"><link rel="prefetch" href="../assets/js/409.aa2afaca.js"><link rel="prefetch" href="../assets/js/41.e7e858c4.js"><link rel="prefetch" href="../assets/js/410.722e5009.js"><link rel="prefetch" href="../assets/js/411.2dbe321f.js"><link rel="prefetch" href="../assets/js/412.39ed2d1e.js"><link rel="prefetch" href="../assets/js/413.a088f973.js"><link rel="prefetch" href="../assets/js/414.87f8d239.js"><link rel="prefetch" href="../assets/js/415.f90a1394.js"><link rel="prefetch" href="../assets/js/416.ea4ba38d.js"><link rel="prefetch" href="../assets/js/417.8a1c9676.js"><link rel="prefetch" href="../assets/js/418.79315748.js"><link rel="prefetch" href="../assets/js/419.1e3ddf12.js"><link rel="prefetch" href="../assets/js/42.8202bb44.js"><link rel="prefetch" href="../assets/js/420.b6d9e0cb.js"><link rel="prefetch" href="../assets/js/421.0c74ef64.js"><link rel="prefetch" href="../assets/js/422.94c20620.js"><link rel="prefetch" href="../assets/js/423.fa0d6946.js"><link rel="prefetch" href="../assets/js/424.9036ac4f.js"><link rel="prefetch" href="../assets/js/425.8d8e82da.js"><link rel="prefetch" href="../assets/js/426.03b7da89.js"><link rel="prefetch" href="../assets/js/427.e3c8c690.js"><link rel="prefetch" href="../assets/js/428.96fdfdce.js"><link rel="prefetch" href="../assets/js/429.a8863be5.js"><link rel="prefetch" href="../assets/js/43.55e30ded.js"><link rel="prefetch" href="../assets/js/430.305aa090.js"><link rel="prefetch" href="../assets/js/431.e02d3725.js"><link rel="prefetch" href="../assets/js/432.83fa148f.js"><link rel="prefetch" href="../assets/js/433.1f01715d.js"><link rel="prefetch" href="../assets/js/434.f3354ec6.js"><link rel="prefetch" href="../assets/js/435.245644ed.js"><link rel="prefetch" href="../assets/js/436.3c933a14.js"><link rel="prefetch" href="../assets/js/437.b62325fb.js"><link rel="prefetch" href="../assets/js/438.8e122783.js"><link rel="prefetch" href="../assets/js/439.e95fa7e6.js"><link rel="prefetch" href="../assets/js/44.e5c90e7a.js"><link rel="prefetch" href="../assets/js/440.5c168090.js"><link rel="prefetch" href="../assets/js/441.f8835ac7.js"><link rel="prefetch" href="../assets/js/442.cf1de5dc.js"><link rel="prefetch" href="../assets/js/443.50ec8b3c.js"><link rel="prefetch" href="../assets/js/444.1600e74f.js"><link rel="prefetch" href="../assets/js/445.795f336d.js"><link rel="prefetch" href="../assets/js/446.1fb71b0f.js"><link rel="prefetch" href="../assets/js/447.0136df19.js"><link rel="prefetch" href="../assets/js/448.abf11aa4.js"><link rel="prefetch" href="../assets/js/449.4f99bee8.js"><link rel="prefetch" href="../assets/js/45.89368cde.js"><link rel="prefetch" href="../assets/js/450.55da381a.js"><link rel="prefetch" href="../assets/js/451.97a68e8d.js"><link rel="prefetch" href="../assets/js/452.9dd8c55e.js"><link rel="prefetch" href="../assets/js/453.3167da0d.js"><link rel="prefetch" href="../assets/js/454.18fad993.js"><link rel="prefetch" href="../assets/js/455.24cbebda.js"><link rel="prefetch" href="../assets/js/456.a2f19b40.js"><link rel="prefetch" href="../assets/js/457.27f4fb5a.js"><link rel="prefetch" href="../assets/js/458.29941084.js"><link rel="prefetch" href="../assets/js/459.b0cc6f89.js"><link rel="prefetch" href="../assets/js/46.1f553681.js"><link rel="prefetch" href="../assets/js/460.86ef4965.js"><link rel="prefetch" href="../assets/js/461.c0b0f695.js"><link rel="prefetch" href="../assets/js/462.8b86c3a9.js"><link rel="prefetch" href="../assets/js/463.79aadbb4.js"><link rel="prefetch" href="../assets/js/464.9735f749.js"><link rel="prefetch" href="../assets/js/465.4bdede2b.js"><link rel="prefetch" href="../assets/js/466.ef630311.js"><link rel="prefetch" href="../assets/js/467.8664e027.js"><link rel="prefetch" href="../assets/js/468.89b3d4ff.js"><link rel="prefetch" href="../assets/js/469.74e0af2c.js"><link rel="prefetch" href="../assets/js/47.5f78034c.js"><link rel="prefetch" href="../assets/js/470.9643f84e.js"><link rel="prefetch" href="../assets/js/471.e3a1e16a.js"><link rel="prefetch" href="../assets/js/472.116791e2.js"><link rel="prefetch" href="../assets/js/473.3bb6cb3d.js"><link rel="prefetch" href="../assets/js/474.1a7a0724.js"><link rel="prefetch" href="../assets/js/475.76c29d71.js"><link rel="prefetch" href="../assets/js/476.fe1fd93a.js"><link rel="prefetch" href="../assets/js/477.e3f203de.js"><link rel="prefetch" href="../assets/js/478.b34f8b1c.js"><link rel="prefetch" href="../assets/js/479.06d48874.js"><link rel="prefetch" href="../assets/js/48.0c8a5846.js"><link rel="prefetch" href="../assets/js/480.ffc77ad4.js"><link rel="prefetch" href="../assets/js/481.2475d5aa.js"><link rel="prefetch" href="../assets/js/482.5da370b4.js"><link rel="prefetch" href="../assets/js/483.2531f6c1.js"><link rel="prefetch" href="../assets/js/484.3f8b3ccf.js"><link rel="prefetch" href="../assets/js/485.fb51e0ee.js"><link rel="prefetch" href="../assets/js/486.881d8443.js"><link rel="prefetch" href="../assets/js/487.36a959cb.js"><link rel="prefetch" href="../assets/js/488.086e154c.js"><link rel="prefetch" href="../assets/js/489.2afd2906.js"><link rel="prefetch" href="../assets/js/49.40a46041.js"><link rel="prefetch" href="../assets/js/490.a517239f.js"><link rel="prefetch" href="../assets/js/491.c1f416dd.js"><link rel="prefetch" href="../assets/js/492.e36de27b.js"><link rel="prefetch" href="../assets/js/493.af28d057.js"><link rel="prefetch" href="../assets/js/494.00509c64.js"><link rel="prefetch" href="../assets/js/495.8ed86db6.js"><link rel="prefetch" href="../assets/js/496.412e9f2d.js"><link rel="prefetch" href="../assets/js/497.ed9f0eeb.js"><link rel="prefetch" href="../assets/js/498.2112afec.js"><link rel="prefetch" href="../assets/js/499.743df60a.js"><link rel="prefetch" href="../assets/js/5.6f1dbbc3.js"><link rel="prefetch" href="../assets/js/50.54de97a0.js"><link rel="prefetch" href="../assets/js/500.ab62befa.js"><link rel="prefetch" href="../assets/js/501.0d54ba9a.js"><link rel="prefetch" href="../assets/js/502.2f3bf836.js"><link rel="prefetch" href="../assets/js/503.c3473b0e.js"><link rel="prefetch" href="../assets/js/504.cb511490.js"><link rel="prefetch" href="../assets/js/505.a2cc8bdc.js"><link rel="prefetch" href="../assets/js/506.f8ea663a.js"><link rel="prefetch" href="../assets/js/507.9cc67e94.js"><link rel="prefetch" href="../assets/js/508.e08708a1.js"><link rel="prefetch" href="../assets/js/509.437502c3.js"><link rel="prefetch" href="../assets/js/51.af5d6a98.js"><link rel="prefetch" href="../assets/js/510.f5e8a583.js"><link rel="prefetch" href="../assets/js/511.6fe511a5.js"><link rel="prefetch" href="../assets/js/512.459d2d68.js"><link rel="prefetch" href="../assets/js/513.a5f4a768.js"><link rel="prefetch" href="../assets/js/514.5fd44c68.js"><link rel="prefetch" href="../assets/js/515.c046ffb4.js"><link rel="prefetch" href="../assets/js/516.a972f386.js"><link rel="prefetch" href="../assets/js/517.7c804920.js"><link rel="prefetch" href="../assets/js/518.ff4a2545.js"><link rel="prefetch" href="../assets/js/519.b1a8d590.js"><link rel="prefetch" href="../assets/js/52.b766b60a.js"><link rel="prefetch" href="../assets/js/520.2d34cada.js"><link rel="prefetch" href="../assets/js/521.f14247fe.js"><link rel="prefetch" href="../assets/js/522.38b72172.js"><link rel="prefetch" href="../assets/js/523.203fa4ac.js"><link rel="prefetch" href="../assets/js/524.236cda03.js"><link rel="prefetch" href="../assets/js/525.8caaad18.js"><link rel="prefetch" href="../assets/js/526.9a217f1e.js"><link rel="prefetch" href="../assets/js/527.06dc5a4f.js"><link rel="prefetch" href="../assets/js/528.fa884c79.js"><link rel="prefetch" href="../assets/js/529.d39c75ca.js"><link rel="prefetch" href="../assets/js/53.eae65ff7.js"><link rel="prefetch" href="../assets/js/530.65b18758.js"><link rel="prefetch" href="../assets/js/531.85af388d.js"><link rel="prefetch" href="../assets/js/532.9d3f253a.js"><link rel="prefetch" href="../assets/js/533.77c2fa0b.js"><link rel="prefetch" href="../assets/js/534.924b0eb2.js"><link rel="prefetch" href="../assets/js/535.5854a4b0.js"><link rel="prefetch" href="../assets/js/536.8b57b538.js"><link rel="prefetch" href="../assets/js/537.ccb7ba0f.js"><link rel="prefetch" href="../assets/js/538.b9c6c631.js"><link rel="prefetch" href="../assets/js/539.63164d8a.js"><link rel="prefetch" href="../assets/js/54.9b89b352.js"><link rel="prefetch" href="../assets/js/540.7e721167.js"><link rel="prefetch" href="../assets/js/541.0cd3f048.js"><link rel="prefetch" href="../assets/js/542.6c5df759.js"><link rel="prefetch" href="../assets/js/543.e1eebdf2.js"><link rel="prefetch" href="../assets/js/544.a3042061.js"><link rel="prefetch" href="../assets/js/545.1a468f89.js"><link rel="prefetch" href="../assets/js/546.892c1841.js"><link rel="prefetch" href="../assets/js/547.c6b5ef7e.js"><link rel="prefetch" href="../assets/js/548.19d884b6.js"><link rel="prefetch" href="../assets/js/549.7c34a773.js"><link rel="prefetch" href="../assets/js/55.b8a1964e.js"><link rel="prefetch" href="../assets/js/550.25f7d121.js"><link rel="prefetch" href="../assets/js/551.b9f92580.js"><link rel="prefetch" href="../assets/js/552.bf452147.js"><link rel="prefetch" href="../assets/js/553.4815ac56.js"><link rel="prefetch" href="../assets/js/554.c253ebb4.js"><link rel="prefetch" href="../assets/js/555.4dbef2bf.js"><link rel="prefetch" href="../assets/js/556.69f1a9f4.js"><link rel="prefetch" href="../assets/js/557.679e8f40.js"><link rel="prefetch" href="../assets/js/558.9515c7f8.js"><link rel="prefetch" href="../assets/js/559.eb17cff7.js"><link rel="prefetch" href="../assets/js/56.88fa6666.js"><link rel="prefetch" href="../assets/js/560.0de96a04.js"><link rel="prefetch" href="../assets/js/561.cdf49465.js"><link rel="prefetch" href="../assets/js/562.2e895986.js"><link rel="prefetch" href="../assets/js/563.40a31d36.js"><link rel="prefetch" href="../assets/js/564.08f15bf2.js"><link rel="prefetch" href="../assets/js/565.e7625c6a.js"><link rel="prefetch" href="../assets/js/566.71ee556d.js"><link rel="prefetch" href="../assets/js/567.7b3e4bc8.js"><link rel="prefetch" href="../assets/js/568.1a3c07dc.js"><link rel="prefetch" href="../assets/js/569.4c496338.js"><link rel="prefetch" href="../assets/js/57.519e80e3.js"><link rel="prefetch" href="../assets/js/570.d152813e.js"><link rel="prefetch" href="../assets/js/571.e46946bd.js"><link rel="prefetch" href="../assets/js/572.ae215a0d.js"><link rel="prefetch" href="../assets/js/573.da7b2e6c.js"><link rel="prefetch" href="../assets/js/574.f068c09e.js"><link rel="prefetch" href="../assets/js/575.f2a128db.js"><link rel="prefetch" href="../assets/js/576.caf5cd68.js"><link rel="prefetch" href="../assets/js/577.b6c04041.js"><link rel="prefetch" href="../assets/js/578.a7707d73.js"><link rel="prefetch" href="../assets/js/579.ed17d056.js"><link rel="prefetch" href="../assets/js/58.963c2309.js"><link rel="prefetch" href="../assets/js/580.88a2ce36.js"><link rel="prefetch" href="../assets/js/581.f99a65fa.js"><link rel="prefetch" href="../assets/js/582.220ce66c.js"><link rel="prefetch" href="../assets/js/583.5d42e8cc.js"><link rel="prefetch" href="../assets/js/584.0737b3ce.js"><link rel="prefetch" href="../assets/js/585.516b187d.js"><link rel="prefetch" href="../assets/js/586.420c42c4.js"><link rel="prefetch" href="../assets/js/587.e3deb3dd.js"><link rel="prefetch" href="../assets/js/588.c9bfc1e3.js"><link rel="prefetch" href="../assets/js/589.ee0f90f3.js"><link rel="prefetch" href="../assets/js/59.d9b10f83.js"><link rel="prefetch" href="../assets/js/590.ea859bba.js"><link rel="prefetch" href="../assets/js/591.4b9781fd.js"><link rel="prefetch" href="../assets/js/592.c9da8cab.js"><link rel="prefetch" href="../assets/js/6.064acd80.js"><link rel="prefetch" href="../assets/js/60.7e89993c.js"><link rel="prefetch" href="../assets/js/61.f2039f26.js"><link rel="prefetch" href="../assets/js/62.474d18a7.js"><link rel="prefetch" href="../assets/js/63.3a64d334.js"><link rel="prefetch" href="../assets/js/64.4a3787c2.js"><link rel="prefetch" href="../assets/js/65.120ee13f.js"><link rel="prefetch" href="../assets/js/66.e7ff207d.js"><link rel="prefetch" href="../assets/js/67.55a68541.js"><link rel="prefetch" href="../assets/js/68.33f8c7cd.js"><link rel="prefetch" href="../assets/js/69.69d65c56.js"><link rel="prefetch" href="../assets/js/7.ce2273f5.js"><link rel="prefetch" href="../assets/js/70.93a544a5.js"><link rel="prefetch" href="../assets/js/71.6eb8d306.js"><link rel="prefetch" href="../assets/js/72.8cbae6a9.js"><link rel="prefetch" href="../assets/js/73.c737f071.js"><link rel="prefetch" href="../assets/js/74.d0ea9fc8.js"><link rel="prefetch" href="../assets/js/75.2219607f.js"><link rel="prefetch" href="../assets/js/76.278079e2.js"><link rel="prefetch" href="../assets/js/77.88eadfbc.js"><link rel="prefetch" href="../assets/js/78.257c919e.js"><link rel="prefetch" href="../assets/js/79.e6f5d078.js"><link rel="prefetch" href="../assets/js/80.40ba7856.js"><link rel="prefetch" href="../assets/js/81.1fab2c8f.js"><link rel="prefetch" href="../assets/js/82.6eaad50e.js"><link rel="prefetch" href="../assets/js/83.9f3e6e81.js"><link rel="prefetch" href="../assets/js/84.060f32c2.js"><link rel="prefetch" href="../assets/js/85.c069396c.js"><link rel="prefetch" href="../assets/js/86.ced213eb.js"><link rel="prefetch" href="../assets/js/87.763c4879.js"><link rel="prefetch" href="../assets/js/88.69fe1a6a.js"><link rel="prefetch" href="../assets/js/89.b87612a8.js"><link rel="prefetch" href="../assets/js/9.5f2fc60c.js"><link rel="prefetch" href="../assets/js/90.6132ccb6.js"><link rel="prefetch" href="../assets/js/91.27dbc3bd.js"><link rel="prefetch" href="../assets/js/92.88832461.js"><link rel="prefetch" href="../assets/js/93.0166bd69.js"><link rel="prefetch" href="../assets/js/94.e29b12f1.js"><link rel="prefetch" href="../assets/js/95.f1245827.js"><link rel="prefetch" href="../assets/js/96.9c45438e.js"><link rel="prefetch" href="../assets/js/97.e8783a06.js"><link rel="prefetch" href="../assets/js/98.60628afa.js"><link rel="prefetch" href="../assets/js/99.b3cb9d0f.js">
    <link rel="stylesheet" href="../assets/css/0.styles.f7de6701.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="../index.html" class="home-link router-link-active"><!----> <span class="site-name">代码重工</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <!----></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><!---->  <!----> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="分布式事务"><a href="#分布式事务" class="header-anchor">#</a> 分布式事务</h1> <h2 id="课程内容"><a href="#课程内容" class="header-anchor">#</a> 课程内容</h2> <ul><li>分布式事务</li> <li>分布式事务解决方案</li> <li>seata</li></ul> <h2 id="_1-分布式事务"><a href="#_1-分布式事务" class="header-anchor">#</a> 1. 分布式事务</h2> <h3 id="_1-0-架构演变对操作可靠性影响的推演"><a href="#_1-0-架构演变对操作可靠性影响的推演" class="header-anchor">#</a> 1.0 架构演变对操作可靠性影响的推演</h3> <h4 id="_1单体架构"><a href="#_1单体架构" class="header-anchor">#</a> ①单体架构</h4> <p>问题：操作不一定成功，也可能失败。这个问题对单体节点内部来说，可以使用『本地事务』来控制。所有操作都成功则提交事务；有任何一个操作失败则回滚事务。数据即使不能进入下一个正确状态，也会回到上一个正确状态。</p> <p>但是放在分布式系统中，单一节点内部的操作有可能是一个总体操作的组成部分。如果总体操作中有的部分成功了，有的部分失败了，这仍然会造成数据的不一致。</p> <p>所以从这里我们能看到分布式系统的第一个不确定因素：整个分布式系统中<span style="color:blue;font-weight:bold;">单一节点的操作有可能失败</span>。而这个情况是<span style="color:blue;font-weight:bold;">无法彻底消除</span>的。例如：库存是 3 的时候，下单数量是 5，下单就是不能成功，因为库存不能减少到负数。</p> <p><img src="" alt="images"></p> <h4 id="_2分布式系统节点之间网络通信"><a href="#_2分布式系统节点之间网络通信" class="header-anchor">#</a> ②分布式系统节点之间网络通信</h4> <p>问题：网络通信有丢失数据的可能。哪怕只有 0.00001% 的风险，也不能说是 100% 的保障。这个不确定性因素也是<span style="color:blue;font-weight:bold;">无法彻底消除</span>的。</p> <p><img src="../assets/img/img002.fa8f65be.png" alt="images"></p> <h4 id="_3结论"><a href="#_3结论" class="header-anchor">#</a> ③结论</h4> <p>导致分布式事务的最基本的因素有两个：</p> <ul><li>单个节点内部操作失败：导致我们从整个系统的视角来看数据进入不一致状态。</li> <li>网络传输失败：导致我们无法确认某个节点的操作是否得到了执行。</li></ul> <p>而最关键的是：<span style="color:blue;font-weight:bold;">从微观上来看</span>，造成分布式事务问题的这两个基本因素<span style="color:blue;font-weight:bold;">无法彻底消除</span>。</p> <p><img src="../assets/img/img003.9c673aee.png" alt="images"></p> <p>所以我们解决分布式事务的问题要有这样一个意识：</p> <ul><li>微观上来看：操作没有办法保证都成功。</li> <li>宏观上来看：即使局部操作失败，整个系统仍然能够正常运行。</li> <li>从程度上来看：分布式事务的问题不是非黑即白的不是成功就是失败，而是有一个程度问题。我们需要根据项目的实际需求，在性能和数据一致性之间取得平衡。</li></ul> <h3 id="_1-1-什么是事务"><a href="#_1-1-什么是事务" class="header-anchor">#</a> 1.1 什么是事务</h3> <p>事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元，组成事务的所有操作只有在所有操作均能正常执行的情况下方能提交，只要其中任一操作执行失败，都将导致整个事务的回滚。</p> <blockquote><p>不知道你是否遇到过这样的情况，去小卖铺买东西，付了钱，但是店主因为处理了一些其他事，居然忘记你付了钱，又叫你重新付。</p> <p>又或者在网上购物明明已经扣款，但是却告诉我没有发生交易。这一系列情况都是因为没有事务导致的。这说明了事务在生活中的一些重要性。</p> <p>有了事务，你去小卖铺买东西，那就是一手交钱一手交货。有了事务，你去网上购物，扣款即产生订单交易。</p></blockquote> <p><em><strong>*简单地说，事务提供一种“要么什么都不做，要么做全套（All or Nothing）”机制。*</strong></em></p> <h3 id="_1-2-事务特性-acid"><a href="#_1-2-事务特性-acid" class="header-anchor">#</a> 1.2 事务特性(ACID)</h3> <p>事务满足ACID特性：</p> <ul><li>A：原子性(Atomicity)</li></ul> <p>一个事务(transaction)中的所有操作，要么全部完成，要么全部不完成，不会结束在中间某个环节。</p> <p>事务在执行过程中发生错误，会被回滚（Rollback）到事务开始前的状态，就像这个事务从来没有执行过一样。</p> <p>就像你买东西要么交钱收货一起都执行，要么发不出货，就退钱。</p> <ul><li>C：一致性(Consistency)</li></ul> <p>事务的一致性指的是在一个事务执行之前和执行之后数据库都必须处于一致性状态。</p> <p>如果事务成功地完成，那么系统中所有变化将正确地应用，系统处于有效状态。</p> <p>如果在事务中出现错误，那么系统中的所有变化将自动地回滚，系统返回到原始状态。</p> <ul><li>I：隔离性(Isolation)</li></ul> <p>指的是在并发环境中，当不同的事务同时操纵相同的数据时，每个事务都有各自的完整数据空间。</p> <p>由并发事务所做的修改必须与任何其他并发事务所做的修改隔离。事务查看数据更新时，数据所处的状态要么是另一事务修改它之前的状态，要么是另一事务修改它之后的状态，事务不会查看到中间状态的数据。</p> <p>打个比方，你买东西这个事情，是不影响其他人的。</p> <ul><li>D：持久性(Durability)</li></ul> <p>指的是只要事务成功结束，它对数据库所做的更新就必须保存下来。即使发生系统崩溃，重新启动数据库系统后，数据库还能恢复到事务成功结束时的状态。</p> <p>打个比方，你买东西的时候需要记录在账本上，即使老板忘记了那也有据可查。</p> <h3 id="_1-3-本地事务"><a href="#_1-3-本地事务" class="header-anchor">#</a> 1.3 本地事务</h3> <ul><li>什么是本地事务（Local Transaction）？</li></ul> <p>本地事务也称为<strong>数据库事务</strong>或<strong>传统事务</strong>（相对于分布式事务而言,执行的多条sql语句在同一个会话连接中）。它的执行模式就是常见的：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token number">1.</span> <span class="token keyword">begin</span>
<span class="token number">2.</span> <span class="token keyword">insert</span><span class="token operator">/</span><span class="token keyword">delete</span><span class="token operator">/</span><span class="token keyword">update</span>
<span class="token number">3.</span> <span class="token keyword">insert</span><span class="token operator">/</span><span class="token keyword">delete</span><span class="token operator">/</span><span class="token keyword">update</span>
<span class="token number">4.</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token number">5.</span> <span class="token keyword">commit</span><span class="token operator">/</span><span class="token keyword">rollback</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li><p>本地事务有这么几个特征：</p> <ul><li>-一次事务只连接一个支持事务的数据库（一般来说都是关系型数据库）</li> <li>事务的执行结果保证ACID</li> <li>会用到数据库锁</li></ul></li> <li><p>起初，事务仅限于对单一数据库资源的访问控制，架构服务化以后，事务的概念延伸到了服务中。倘若将一个单一的服务操作作为一个事务，那么整个服务操作只能涉及一个单一的数据库资源，这类基于单个服务单一数据库资源访问的事务，被称为本地事务(Local Transaction)。</p> <p><img src="../assets/img/image-20210927110449421.1f44173f.png" alt="image-20210927110449421"></p></li></ul> <h3 id="_1-4-什么是分布式事务"><a href="#_1-4-什么是分布式事务" class="header-anchor">#</a> 1.4 什么是分布式事务</h3> <p>分布式事务指事务的参与者、支持事务的服务器、资源服务器以及事务管理器分别位于分布式系统的不同节点上，且属于不同的应用。</p> <p>指一次大的操作由不同的小操作组成的，这些小的操作分布在不同的服务器上，分布式事务需要保证这些小操作要么全部成功，要么全部失败。</p> <p>本质上来说，分布式事务就是为了保证不同数据库的数据一致性。</p> <p><img src="../assets/img/image-20210927111458186.2b6e4639.png" alt="image-20210927111458186"></p> <blockquote><p>上图是一个电商支付流程简化图，整个平台按照微服务进行拆分，涉及到服务模块有订单、商品、用户等模块。用户从浏览器选择好商品后进行支付下单操作，支付下单在订单模块生成。当订单成功生成后就需要分别调用商品模块进行扣减商品库存以及用户模块给用户增加积分等操作。这种架构下的事务就属于分布式事务了，要么全部业务模块都成功执行并提交数据库。只要其中一个微服务出现异常整个调用链路涉及到的操作都需要进行回滚，保障事务的一致性。</p></blockquote> <h3 id="_1-5-分布式事务场景"><a href="#_1-5-分布式事务场景" class="header-anchor">#</a> 1.5 分布式事务场景</h3> <ul><li>电商系统中的下单扣库存</li></ul> <p>​	电商系统中，****订单系统*<em><strong>和</strong></em>*库存系统****是两个系统，一次下单的操作由两个系统协同完成</p> <ul><li>金融系统中的银行卡充值</li></ul> <p>​	在金融系统中通过银行卡向平台充值需要通过****银行系统*<em><strong>和</strong></em>*金融系统****协同完成。</p> <h2 id="_2、分布式事务标准与规范"><a href="#_2、分布式事务标准与规范" class="header-anchor">#</a> 2、分布式事务标准与规范</h2> <p>通过前面的分析我们可以了解到，分布式事务面临的问题无法达到绝对的、100% 可靠，没有办法从微观上、根源上彻底解决。那么问题解决到什么程度？性能和数据准确如何取舍和平衡？等等这些两难问题我们都需要有相关的标准和规范来参照和衡量。</p> <h3 id="_1cap-定理"><a href="#_1cap-定理" class="header-anchor">#</a> ①CAP 定理</h3> <p>CAP 定理，又叫做布鲁尔定理。对于设计分布式系统（不仅仅是分布式事务）的架构师来说，CAP 就是你的入门理论。</p> <p>分布式系统（Distribution System）正变得越来越重要，大型网站几乎都是分布式的。</p> <p>分布式系统的最大难点就是各节点的状态如何同步。CAP 定理是这方面的基本定理，也是理解分布式系统的起点。</p> <p><img src="../assets/img/CAP.fb1dc229.jpg" alt="images"></p> <h4 id="_1-c-一致性-通俗理解-对"><a href="#_1-c-一致性-通俗理解-对" class="header-anchor">#</a> [1]C：一致性（通俗理解：对）</h4> <p><span style="color:blue;font-weight:bold;">Consistency 一致性</span>：指数据在多个副本之间能够保持一致的特性。</p> <ul><li>强一致性：分布式系统中的数据在任何一个相同的时刻具有同样的值。</li> <li>弱一致性：数据更新后，能够容忍数据在短暂的一段时间内可以不一致。也可称之为：最终一致性。</li></ul> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>数据的『正本』和『副本』：</p> <p>正本：通常 MySQL 这样的关系型数据库中存储的数据视为正本。</p> <p>副本：通常 Redis、ElasticSearch 等 NoSQL 数据库中存储的数据视为副本。</p></div> <h4 id="_2-a-可用性-通俗理解-快"><a href="#_2-a-可用性-通俗理解-快" class="header-anchor">#</a> [2]A：可用性（通俗理解：快）</h4> <p><span style="color:blue;font-weight:bold;">Available 可用性</span>：这是在系统工作过程中，衡量系统<span style="color:blue;font-weight:bold;">性能</span>的一个指标。指系统提供的服务必须一直处于可用的状态，每次只要收到用户的请求，服务器就必须给出回应。在合理的时间内返回合理的响应——不是错误和超时的响应。</p> <p>只有非故障节点才能满足业务正常。</p> <p>只有在合理的时间内用户才能接受。</p> <p>只有返回合理的响应，信息才有价值。</p> <h4 id="_3-p-网络分区容错性-通俗理解-能用"><a href="#_3-p-网络分区容错性-通俗理解-能用" class="header-anchor">#</a> [3]P：网络分区容错性（通俗理解：能用）</h4> <p><span style="color:blue;font-weight:bold;">Partition Toleration 分区容错性</span></p> <ul><li>分区</li></ul> <p>网络节点之间无法通信的情况下，节点被隔离，产生了网络分区，此时整个系统仍然是可以工作的。大多数分布式系统都分布在多个子网络。每个子网络就叫做一个区（partition）。</p> <ul><li>分区容错</li></ul> <p>即使各子网络之间无法通信，甚至某个分区出现了故障，但整个系统对外仍然是可用的。</p> <h4 id="_4-cap-定理"><a href="#_4-cap-定理" class="header-anchor">#</a> [4]CAP 定理</h4> <p>CAP 中的三者不可能同时满足。通常来说 P 是要求必须实现的：从外部视角来看，不管系统内部如何，我们无法接受整个系统对外不可用。在满足 P 的基础上，CP 和 AP 二选一。CAP 不可能同时满足。</p> <h3 id="_2base-定理"><a href="#_2base-定理" class="header-anchor">#</a> ②BASE 定理</h3> <p>BASE = <span style="color:blue;font-weight:bold;">B</span>saically <span style="color:blue;font-weight:bold;">A</span>vailable（基本可用） + <span style="color:blue;font-weight:bold;">S</span>oft state（软状态）+ <span style="color:blue;font-weight:bold;">E</span>ventually consistent（最终一致性）。</p> <p>理论的提出者是 eBay 的架构师。</p> <p>BASE 定理是对 CAP 定理的进一步探讨。既然 CAP 中『一致性』和『可用性』无法同时满足，那如何在二者间达到一个可以接受的平衡呢？</p> <p>BASE 定理的主张是：哪怕是做不到强一致性，也应该尽量做到最终一致性。</p> <h4 id="_1-基本可用"><a href="#_1-基本可用" class="header-anchor">#</a> [1]基本可用</h4> <p>相对于『用户请求什么就返回什么』的完整功能来说，基本可用是指系统在无法准确返回用户所请求的数据时也返回相关数据，而不是『什么都不返回』。</p> <p>相对于『收到用户请求就立即返回结果』的优质用户体验来说，基本可用是指系统在无法立即响应用户请求时，稍作延迟仍然能够返回响应，而不是『放弃响应』。</p> <p>也就是说：服务可以适度<span style="color:blue;font-weight:bold;">『降级』</span>，但不能完全不可用。</p> <h4 id="_2-软状态"><a href="#_2-软状态" class="header-anchor">#</a> [2]软状态</h4> <p>满足 ACID 的事务属性就达到了『硬状态』。在传统的关系型数据库中，事务要么提交，要么回滚——非黑即白。而『软状态』允许系统暂时处于中间状态：从一个操作的发起到它同步到各个分布式节点，允许存在这样一个『正在同步数据』的中间过程。</p> <h4 id="_3-最终一致性"><a href="#_3-最终一致性" class="header-anchor">#</a> [3]最终一致性</h4> <p>相对于关系型数据库事务的『立即一致』，分布式系统应该追求『最终一致』，允许系统在同步数据的过程中存在暂时的数据不一致状态，只要数据能够达到『最终一致』，我们就可以认为系统是可靠的。</p> <h3 id="_3小结"><a href="#_3小结" class="header-anchor">#</a> ③小结</h3> <p>不管 CAP 还是 BASE，它们都只是标准。它们没有提供分布式事务问题的解决方案，但是它们是解决方案是否成立的衡量标准。</p> <h2 id="_3、分布式事务解决方案"><a href="#_3、分布式事务解决方案" class="header-anchor">#</a> 3、分布式事务解决方案</h2> <h3 id="_1基于-xa-协议的两阶段提交-2pc"><a href="#_1基于-xa-协议的两阶段提交-2pc" class="header-anchor">#</a> ①基于 XA 协议的两阶段提交 (2PC)</h3> <p>X/Open 组织（即现在的 Open Group ）定义了分布式事务处理模型</p> <p><strong>XA协议</strong>：XA是一个分布式事务协议。XA中大致分为两部分：事务管理器和本地资源管理器。其中本地资源管理器往往由数据库实现，比如Oracle、DB2这些商业数据库都实现了XA接口，而事务管理器作为全局的调度者，负责各个本地资源的提交和回滚。</p> <h4 id="_2-1-1-概念"><a href="#_2-1-1-概念" class="header-anchor">#</a> 2.1.1 概念</h4> <p>二阶段提交2PC（Two phase Commit）是指，在分布式系统里，为了保证所有节点在进行事务提交时保持一致性的一种算法。</p> <h4 id="_2-1-2-背景"><a href="#_2-1-2-背景" class="header-anchor">#</a> 2.1.2 背景</h4> <p>在分布式系统里，<strong>每个节点都可以知晓自己操作的成功或者失败，却无法知道其他节点操作的成功或失败。</strong></p> <p>当一个事务跨多个节点时，为了保持事务的原子性与一致性，需要引入一个<strong>协调者</strong>（Coordinator）来统一掌控所有<strong>参与者</strong>（Participant）的操作结果，并指示它们是否要把操作结果进行真正的提交（commit）或者回滚（rollback）。</p> <h4 id="_2-1-3-思路"><a href="#_2-1-3-思路" class="header-anchor">#</a> 2.1.3 思路</h4> <p>2PC顾名思义分为两个阶段，其实施思路可概括为：</p> <p>（1）投票阶段（voting phase）：参与者将操作结果通知协调者；</p> <p>（2）提交阶段（commit phase）：收到参与者的通知后，协调者再向参与者发出通知，根据反馈情况决定各参与者是否要提交还是回滚；</p> <h4 id="_2-1-4-缺陷"><a href="#_2-1-4-缺陷" class="header-anchor">#</a> 2.1.4 缺陷</h4> <p>算法执行过程中，<strong>所有节点都处于阻塞状态，所有节点所持有的资源（例如数据库数据，本地文件等）都处于封锁状态。</strong></p> <p>典型场景为：</p> <p>（1）某一个参与者发出通知之前，所有参与者以及协调者都处于阻塞状态；</p> <p>（2）在协调者发出通知之前，所有参与者都处于阻塞状态；</p> <p>另外，如有协调者或者某个参与者出现了崩溃，为了避免整个算法处于一个完全阻塞状态，往往需要借助超时机制来将算法继续向前推进，故此时算法的效率比较低。</p> <p>总的来说，<strong>2PC是一种比较保守的算法</strong>。</p> <h4 id="_2-1-5-举例"><a href="#_2-1-5-举例" class="header-anchor">#</a> 2.1.5 举例</h4> <p>甲乙丙丁四人要组织一个会议，需要确定会议时间，不妨设甲是协调者，乙丙丁是参与者。</p> <p><strong>投票阶段：</strong></p> <p>（1）甲发邮件给乙丙丁，周二十点开会是否有时间；</p> <p>（2）乙回复有时间；</p> <p>（3）丙回复有时间；</p> <p>（4）丁迟迟不回复，此时对于这个活动，甲乙丙均处于阻塞状态，算法无法继续进行；</p> <ul><li>甲：在等待丁的回复</li> <li>乙：在等待甲的下一个通知</li> <li>丙：在等待甲的下一个通知</li></ul> <p>（5）丁回复有时间（或者没有时间）；</p> <ul><li>此时甲收集到了全部反馈信息：乙（成功），丙（成功），丁（失败）</li> <li>甲通知下一步操作：会议取消</li></ul> <p><strong>提交阶段：</strong></p> <p>（1）协调者甲将收集到的结果反馈给乙丙丁（什么时候反馈，以及反馈结果如何，在此例中取决与丁的时间与决定）；</p> <p>（2）乙收到；（收到通知，会议取消）</p> <p>（3）丙收到；（收到通知，会议取消）</p> <p>（4）丁收到；（收到通知，会议取消）</p> <h4 id="_2-1-6-结论"><a href="#_2-1-6-结论" class="header-anchor">#</a> 2.1.6 结论</h4> <p>2PC效率很低，分布式事务很难做</p> <h4 id="_2-1-7-实际应用交互流程"><a href="#_2-1-7-实际应用交互流程" class="header-anchor">#</a> 2.1.7 实际应用交互流程</h4> <ul><li>2PC两阶段提交的正向流程（最终能够提交事务）</li></ul> <p><strong>第一阶段：</strong></p> <p>2PC中包含着两个角色：<strong>事务协调者</strong>和<strong>事务参与者</strong>。让我们来看一看他们之间的交互流程：</p> <p><img src="../assets/img/wpsD4A0.tmp.bd5469e1.jpg" alt="img"></p> <p>在分布式事务的第一阶段，作为事务协调者的节点会首先向所有的参与者节点发送Prepare请求。</p> <p>在接到Prepare请求之后，每一个参与者节点会各自执行与事务有关的数据更新，写入Undo Log和Redo Log。如果参与者执行成功，暂时不提交事务，而是向事务协调节点返回“完成”消息。</p> <p>当事务协调者接到了所有参与者的返回消息，整个分布式事务将会进入第二阶段。</p> <p><strong>第二阶段：</strong></p> <p><img src="../assets/img/wpsD4A1.tmp.c20bc11f.jpg" alt="img"></p> <p>在2PC分布式事务的第二阶段，如果事务协调节点在之前所收到都是正向返回，那么它将会向所有事务参与者发出Commit请求。</p> <p>接到Commit请求之后，事务参与者节点会各自进行本地的事务提交，并释放锁资源。当本地事务完成提交后，将会向事务协调者返回“完成”消息。</p> <p>当事务协调者接收到所有事务参与者的“完成”反馈，整个分布式事务完成。</p> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>ACK 的含义是：Acknowledge character，确认字符。</p></div> <ul><li>失败情况的处理流程（事务最终会回滚）</li></ul> <p><em><strong>*第一阶段：*</strong></em></p> <p><img src="../assets/img/wpsD4B1.tmp.3c1aa7f9.jpg" alt="img"></p> <p><em><strong>*第二阶段：*</strong></em></p> <p><img src="../assets/img/wpsD4B2.tmp.c18f661d.jpg" alt="img"></p> <p>在2PC的第一阶段，如果某个事务参与者反馈失败消息，说明该节点的本地事务执行不成功，必须回滚。</p> <p>于是在第二阶段，事务协调节点向所有的事务参与者发送Abort(中止)请求。接收到Abort请求之后，各个事务参与者节点需要在本地进行事务的回滚操作，回滚操作依照Undo Log来进行。</p> <p>以上就是2PC两阶段提交协议的详细过程。</p> <ul><li>2PC两阶段提交究竟有哪些不足呢？</li></ul> <p><strong>1)性能问题</strong></p> <p>2PC遵循强一致性。在事务执行过程中，各个节点占用着数据库资源，只有当所有节点准备完毕，事务协调者才会通知提交，参与者提交后释放资源。这样的过程有着非常明显的性能问题。</p> <p><strong>2) 协调者单点故障问题</strong></p> <p>协调者是整个2PC模型的核心，一旦事务协调者节点挂掉，参与者收不到提交或是回滚通知，参与者会一直处于中间状态无法完成事务。</p> <p><strong>3) 丢失消息导致的不一致问题。</strong></p> <p>的第二个阶段，如果发生局部网络问题，一部分事务参与者收到了提交消息，另一部分事务参与者没收到提交消息，那么就导致了节点之间数据的不一致。</p> <h3 id="_2代码补偿事务-tcc"><a href="#_2代码补偿事务-tcc" class="header-anchor">#</a> ②代码补偿事务(TCC）</h3> <p>TCC 的作用主要是解决跨服务调用场景下的分布式事务问题</p> <h4 id="_2-2-1-场景案例"><a href="#_2-2-1-场景案例" class="header-anchor">#</a> 2.2.1 场景案例</h4> <p>以航班预定的案例，来介绍TCC要解决的事务场景。在这里笔者虚构一个场景，把自己当做航班预定的主人公，来介绍这个案例。从合肥 –&gt; 昆明 –&gt; 大理。</p> <p>准备从合肥出发，到云南大理去游玩，然后使用美团App(机票代理商)来订机票。发现没有从合肥直达大理的航班，需要到昆明进行中转。如下图：</p> <p><img src="../assets/img/wpsF260.tmp.682c5c92.jpg" alt="img"></p> <p>从图中我们可以看出来，从合肥到昆明乘坐的是四川航空，从昆明到大理乘坐的是东方航空。</p> <p>由于使用的是美团App预定，当我选择了这种航班预定方案后，美团App要去四川航空和东方航空各帮我购买一张票。如下图：</p> <p><img src="../assets/img/wpsF261.tmp.682a3809.jpg" alt="img"></p> <p>考虑最简单的情况：美团先去川航帮我买票，如果买不到，那么东航也没必要买了。如果川航购买成功，再去东航购买另一张票。</p> <p>现在问题来了：假设美团先从川航成功买到了票，然后去东航买票的时候，因为天气问题，东航航班被取消了。那么此时，美团必须取消川航的票，因为只有一张票是没用的，不取消就是浪费我的钱。那么如果取消会怎样呢？如果读者有取消机票经历的话，非正常退票，肯定要扣手续费的。在这里，川航本来已经购买成功，现在因为东航的原因要退川航的票，川航应该是要扣代理商的钱的。</p> <p>那么美团就要保证，如果任一航班购买失败，都不能扣钱，怎么做呢？</p> <p>两个航空公司都为美团提供以下3个接口：<strong>机票预留接口、确认接口、取消接口</strong>。美团App分2个阶段进行调用，如下所示：</p> <p><img src="../assets/img/wpsF262.tmp.264ed4e9.jpg" alt="img"></p> <p><em><strong>*在第 1 阶段：*</strong></em></p> <p>美团分别请求两个航空公司预留机票，两个航空公司分别告诉美团预留成功还是失败。航空公司需要保证，机票预留成功的话，之后一定能购买到。</p> <p><em><strong>*在第 2 阶段：*</strong></em></p> <p>如果两个航空公司都预留成功，则分别向两个公司发送确认购买请求。</p> <p>如果两个航空公司任意一个预留失败，则对于预留成功的航空公司也要取消预留。这种情况下，对于之前预留成功机票的航班取消，也不会扣用户的钱，因为购买并没实际发生，之前只是请求预留机票而已。</p> <p>通过这种方案，可以保证两个航空公司购买机票的一致性，要不都成功，要不都失败，即使失败也不会扣用户的钱。如果在两个航班都已经已经确认购买后，再退票，那肯定还是要扣钱的。</p> <p>当然，实际情况肯定这里提到的肯定要复杂，通常航空公司在第一阶段，对于预留的机票，会要求在指定的时间必须确认购买(支付成功)，如果没有及时确认购买，会自动取消。假设川航要求10分钟内支付成功，东航要求30分钟内支付成功。以较短的时间算，如果用户在10分钟内支付成功的话，那么美团会向两个航空公司都发送确认购买的请求，如果超过10分钟(以较短的时间为准)，那么就不能进行支付。</p> <p>这个方案提供给我们一种跨服务保证事务一致性的一种解决思路，可以把这种方案当做TCC的雏形。</p> <p>TCC是Try ( 尝试 ) — Confirm(确认) — Cancel ( 取消 ) 的简称:</p> <table><thead><tr><th><strong>操作方法</strong></th> <th><strong>含义</strong></th></tr></thead> <tbody><tr><td>Try</td> <td>完成所有业务检查（一致性），预留业务资源(准隔离性) 回顾上面航班预定案例的阶段1，机票就是业务资源，所有的资源提供者(航空公司)预留都成功，try阶段才算成功</td></tr> <tr><td>Confirm</td> <td>确认执行业务操作，不做任何业务检查， 只使用Try阶段预留的业务资源。回顾上面航班预定案例的阶段2，美团APP确认两个航空公司机票都预留成功，因此向两个航空公司分别发送确认购买的请求。</td></tr> <tr><td>Cancel</td> <td>取消Try阶段预留的业务资源。回顾上面航班预定案例的阶段2，如果某个业务方的业务资源没有预留成功，则取消所有业务资源预留请求。</td></tr></tbody></table> <p>有哥们立马会想到，TCC与XA两阶段提交有着异曲同工之妙，下图列出了二者之间的对比：</p> <p><img src="../assets/img/wpsF263.tmp.844aa590.jpg" alt="img"></p> <p><strong>1) 在阶段1：</strong></p> <p>在XA中，各个RM准备提交各自的事务分支，事实上就是准备提交资源的更新操作(insert、delete、update等)；而在TCC中，是主业务活动请求(try)各个从业务服务预留资源。</p> <p><strong>2) 在阶段2：</strong></p> <p>XA根据第一阶段每个RM是否都prepare成功，判断是要提交还是回滚。如果都prepare成功，那么就commit每个事务分支，反之则rollback每个事务分支。</p> <p>TCC中，如果在第一阶段所有业务资源都预留成功，那么confirm各个从业务服务，否则取消(cancel)所有从业务服务的资源预留请求。</p> <h4 id="_2-2-2-tcc两阶段提交与xa两阶段提交的区别"><a href="#_2-2-2-tcc两阶段提交与xa两阶段提交的区别" class="header-anchor">#</a> 2.2.2 TCC两阶段提交与XA两阶段提交的区别</h4> <p>XA是资源层面（直接操作数据库）的分布式事务，强一致性，在两阶段提交的整个过程中，一直会持有资源的锁。</p> <p>XA事务中的两阶段提交内部过程是对开发者屏蔽的 ，其内部会委派给TransactionManager进行真正的两阶段提交，因此开发者从代码层面是感知不到这个过程的。而且事务管理器在两阶段提交过程中，从prepare到commit/rollback过程中，资源实际上一直都是被加锁的。如果有其他人需要更新这两条记录，那么就必须等待锁释放。</p> <p>TCC是业务层面（操作Java服务）的分布式事务，最终一致性，不会一直持有资源的锁。</p> <p>TCC中的两阶段提交并没有对开发者完全屏蔽，也就是说从代码层面，开发者是可以感受到两阶段提交的存在。如上述航班预定案例：在第一阶段，航空公司需要提供<strong>try接口</strong>(机票资源预留)。在第二阶段，航空公司提需要提供<strong>confirm/cancel接口</strong>(确认购买机票/取消预留)。开发者明显的感知到了两阶段提交过程的存在。try、confirm/cancel在执行过程中，一般都会开启各自的本地事务，来保证方法内部业务逻辑的ACID特性。其中：</p> <p>1、try过程的本地事务，是保证资源预留的业务逻辑的正确性。</p> <p>2、confirm/cancel执行的本地事务逻辑确认/取消预留资源，以保证最终一致性，也就是所谓的补偿型事务</p> <p>由于是多个独立的本地事务，因此不会对资源一直加锁。</p> <p>TCC 将事务提交分为 Try - Confirm - Cancel 3个操作。这和两阶段提交有点类似，Try为第一阶段，Confirm - Cancel为第二阶段，是一种应用层面侵入业务的两阶段提交。</p> <table><thead><tr><th><strong>操作方法</strong></th> <th>含义</th></tr></thead> <tbody><tr><td>Try</td> <td>预留业务资源/数据效验-尝试检查当前操作是否可行</td></tr> <tr><td>Confirm</td> <td>确认执行业务操作，实际提交数据，不做任何业务检查，try成功，confirm必定成功</td></tr> <tr><td>Cancel</td> <td>取消执行业务操作，实际回滚数据</td></tr></tbody></table> <p>其核心在于将业务分为两个操作步骤完成。不依赖 RM 对分布式事务的支持，而是通过对业务逻辑的分解来实现分布式事务。</p> <p><img src="../assets/img/wpsF264.tmp.ea2f7bec.jpg" alt="img"></p> <p>例如： A要向 B 转账，思路大概是：</p> <p>假设用户user表中有两个字段：可用余额(available_money)、冻结余额(frozen_money) A扣钱对应服务A(ServiceA) B加钱对应服务B(ServiceB) 转账订单服务(OrderService) 业务转账方法服务(BusinessService)</p> <p>ServiceA，ServiceB，OrderService都需分别实现try()，confirm()，cancle()方法，方法对应业务逻辑如下</p> <p><img src="../assets/img/wpsF265.tmp.08ceb6b7.jpg" alt="img"></p> <p>其中业务调用方BusinessService中就需要调用</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">ServiceA</span><span class="token punctuation">.</span><span class="token keyword">try</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token class-name">ServiceB</span><span class="token punctuation">.</span><span class="token keyword">try</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token class-name">OrderService</span><span class="token punctuation">.</span><span class="token keyword">try</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>1、当所有try()方法均执行成功时，对全局事务进行提交，即由事务管理器调用每个微服务的confirm()方法</p> <p>2、 当任意一个方法try()失败(预留资源不足，抑或网络异常，代码异常等任何异常)，由事物管理器调用每个微服务的cancle()方法对全局事务进行回滚</p> <p>优点： 跟2PC(很多第三方框架)比起来，实现以及流程相对简单了一些，但数据的一致性比2PC也要差一些</p> <p>缺点： 缺点还是比较明显的，在2,3步中都有可能失败。TCC属于应用层的一种补偿方式，所以需要程序员在实现的时候多写很多补偿的代码，在一些场景中，一些业务流程可能用TCC不太好定义及处理。</p> <h3 id="_3本地消息表-异步确保-事务最终一致性"><a href="#_3本地消息表-异步确保-事务最终一致性" class="header-anchor">#</a> ③本地消息表（异步确保）- 事务最终一致性</h3> <p>这种实现方式的思路，其实是源于 ebay，后来通过支付宝等公司的布道，在业内广泛使用。<strong>其基本的设计思想是将远程分布式事务拆分成一系列的本地事务</strong>。如果不考虑性能及设计优雅，借助关系型数据库中的表即可实现。</p> <p>举个经典的跨行转账的例子来描述。</p> <p>第一步伪代码如下，扣款 1W，通过本地事务保证了凭证消息插入到消息表中。</p> <p><img src="../assets/img/wps5410.tmp.1d8c1c00.jpg" alt="img"></p> <p>第二步，通知对方银行账户加 1W。那问题来了，如何通知到对方呢？</p> <ul><li><p>通常采用两种方式：</p> <ul><li><ol><li>采用时效性高的 MQ，由对方订阅消息并监听，有消息时自动触发事件</li></ol></li> <li><ol start="2"><li>采用定时轮询扫描的方式，去检查消息表的数据</li></ol></li></ul></li> <li><p>两种方式其实各有利弊，仅仅依靠 MQ，可能会出现通知失败的问题。而过于频繁的定时轮询，效率也不是最佳的（90% 是无用功）。所以，我们一般会把两种方式结合起来使用。</p></li> <li><p>解决了通知的问题，又有新的问题了。万一这消息有重复被消费，往用户帐号上多加了钱，那岂不是后果很严重？</p></li> <li><p>仔细思考，其实我们可以在消息消费方，也通过一个“消费状态表”来记录消费状态。在执行“加款”操作之前，检测下该消息（提供标识）是否已经消费过，消费完成后，通过本地事务控制来更新这个“消费状态表”。这样子就避免重复消费的问题。</p></li> <li><p>总结：上述的方式是一种非常经典的实现，基本避免了分布式事务，实现了“最终一致性”。但是，关系型数据库的吞吐量和性能方面存在瓶颈，频繁的读写消息会给数据库造成压力。所以，在真正的高并发场景下，该方案也会有瓶颈和限制的。</p></li> <li><p>在订单系统新增一条消息表，将新增订单和新增消息放到一个事务里完成，然后通过轮询的方式去查询消息表，将消息推送到 MQ，库存系统去消费 MQ。</p></li></ul> <p><img src="../assets/img/wps5411.tmp.1a417fb3.jpg" alt="img"></p> <ul><li><p>执行流程：</p> <ul><li>订单系统，添加一条订单和一条消息，在一个事务里提交。</li> <li>订单系统，使用定时任务轮询查询状态为未同步的消息表，发送到 MQ，如果发送失败，就重试发送。</li> <li>库存系统，接收 MQ 消息，修改库存表，需要保证幂等操作。</li> <li>如果修改成功，调用 RPC 接口修改订单系统消息表的状态为已完成或者直接删除这条消息。</li> <li>如果修改失败，可以不做处理，等待重试。</li> <li>订单系统中的消息有可能由于业务问题会一直重复发送，所以为了避免这种情况可以记录一下发送次数，当达到次数限制之后报警，人工接入处理；库存系统需要保证幂等，避免同一条消息被多次消费造成数据不一致。</li> <li>本地消息表这种方案实现了最终一致性，需要在业务系统里增加消息表，业务逻辑中多一次插入的 DB 操作，所以性能会有损耗，而且最终一致性的间隔主要由定时任务的间隔时间决定。</li></ul></li> <li><p>优点： 一种非常经典的实现，避免了分布式事务，实现了最终一致性。在 .NET中有现成的解决方案。</p></li> <li><p>缺点： 消息表会耦合到业务系统中，如果没有封装好的解决方案，会有很多杂活需要处理。</p></li></ul> <h3 id="_4mq-事务消息"><a href="#_4mq-事务消息" class="header-anchor">#</a> ④MQ 事务消息</h3> <p>有一些第三方的MQ是支持事务消息的，比如RocketMQ，他们支持事务消息的方式也是类似于采用的二阶段提交，但是市面上一些主流的MQ都是不支持事务消息的，比如 RabbitMQ 和 Kafka 都不支持，但是Rabbit MQ可以通过<span style="color:blue;font-weight:bold;">可靠消息投递</span>来实现事务消息。</p> <ul><li><p>RocketMQ</p> <p><img src="../assets/img/rocketmq_design_10.4dfdf7d9.png" alt="img"></p></li> <li><p>Rabbit MQ</p> <p><img src="../assets/img/image-20210927115442091.74f20e8e.png" alt="image-20210927115442091"></p></li></ul> <h3 id="_5方案选型"><a href="#_5方案选型" class="header-anchor">#</a> ⑤方案选型</h3> <ul><li><p>基于XA</p> <p>​	依赖于数据库底层XA支持，拿mysql数据库来说，innodb引擎支持XA协议，可以实现分布式事务。但是使用XA事务需要将事务<span style="color:blue;font-weight:bold;">隔离级别设置为串行</span>，这样整个数据库使用性能将下降严重。</p></li> <li><p>TCC</p> <p>业务代码侵入性高，需要提供try、commit、cancel多个接口业务实现。现有支持的开源框架有：ByteTCC、tcc-transaction、seata、tx-lcn等</p></li> <li><p>MQ</p> <p>非金融类业务首选MQ实现。MQ不能及时保障数据一致性，也是一种柔性事务(允许有数据有中间状态，可以保障经过一定时间后最终达到数据一致性)</p></li> <li><p>业务2PC</p> <p>XA协议是从资源层面的实现了2PC，如:seata、tx-lcn等是从业务层面实现了2PC。</p></li></ul> <p><strong>本课程我们使用的是seata作为我们的分布式事务解决方案。</strong></p> <h2 id="_3-seata简介"><a href="#_3-seata简介" class="header-anchor">#</a> 3. Seata简介</h2> <p>Seata是阿里开源的一个分布式事务框架，能够让大家在操作分布式事务时，像操作本地事务一样简单。一个注解搞定分布式事务。</p> <p>官网: https://seata.io/zh-cn/docs/overview/what-is-seata.html</p> <blockquote><p>Seata 是一款开源的分布式事务解决方案，致力于提供高性能和简单易用的分布式事务服务。Seata 为用户提供了 AT、TCC、SAGA 和 XA 事务模式，为用户打造一站式的分布式解决方案。</p></blockquote> <h4 id="at-模式"><a href="#at-模式" class="header-anchor">#</a> AT 模式</h4> <p><img src="../assets/img/aaaaa.c278e4ed.jpg" alt="Typical Process"></p> <ul><li><p>TC (Transaction Coordinator) - 事务协调者</p> <p>​	维护全局和分支事务的状态，驱动全局事务提交或回滚。</p></li> <li><p>TM (Transaction Manager) - 事务管理器</p> <p>​	定义全局事务的范围：开始全局事务、提交或回滚全局事务。</p></li> <li><p>RM (Resource Manager) - 资源管理器</p> <p>​	管理分支事务处理的资源，与TC交谈以注册分支事务和报告分支事务的状态，并驱动分支事务提交或回滚。</p></li></ul> <h5 id="前提"><a href="#前提" class="header-anchor">#</a> 前提</h5> <ul><li>基于支持本地 ACID 事务的关系型数据库。</li> <li>Java 应用，通过 JDBC 访问数据库。</li></ul> <h5 id="整体机制"><a href="#整体机制" class="header-anchor">#</a> 整体机制</h5> <p>两阶段提交协议的演变：</p> <ul><li>一阶段：业务数据和回滚日志记录在同一个本地事务中提交，释放本地锁和连接资源。</li> <li>二阶段：
<ul><li>提交异步化，非常快速地完成。</li> <li>回滚通过一阶段的回滚日志进行反向补偿。</li></ul></li></ul> <h5 id="写隔离"><a href="#写隔离" class="header-anchor">#</a> 写隔离</h5> <ul><li>一阶段本地事务提交前，需要确保先拿到 <strong>全局锁</strong> 。</li> <li>拿不到 <strong>全局锁</strong> ，不能提交本地事务。</li> <li>拿 <strong>全局锁</strong> 的尝试被限制在一定范围内，超出范围将放弃，并回滚本地事务，释放本地锁。</li></ul> <p>以一个示例来说明：</p> <p>两个全局事务 tx1 和 tx2，分别对 a 表的 m 字段进行更新操作，m 的初始值 1000。</p> <p>tx1 先开始，开启本地事务，拿到本地锁，更新操作 m = 1000 - 100 = 900。本地事务提交前，先拿到该记录的 <strong>全局锁</strong> ，本地提交释放本地锁。 tx2 后开始，开启本地事务，拿到本地锁，更新操作 m = 900 - 100 = 800。本地事务提交前，尝试拿该记录的 <strong>全局锁</strong> ，tx1 全局提交前，该记录的全局锁被 tx1 持有，tx2 需要重试等待 <strong>全局锁</strong> 。</p> <p><img src="" alt="Write-Isolation: Commit"></p> <p>tx1 二阶段全局提交，释放 <strong>全局锁</strong> 。tx2 拿到 <strong>全局锁</strong> 提交本地事务。</p> <p><img src="../../../img.alicdn.com/tfs/TB1xW0UwubviK0jSZFNXXaApXXa-718-521.png" alt="Write-Isolation: Rollback"></p> <p>如果 tx1 的二阶段全局回滚，则 tx1 需要重新获取该数据的本地锁，进行反向补偿的更新操作，实现分支的回滚。</p> <p>此时，如果 tx2 仍在等待该数据的 <strong>全局锁</strong>，同时持有本地锁，则 tx1 的分支回滚会失败。分支的回滚会一直重试，直到 tx2 的 <strong>全局锁</strong> 等锁超时，放弃 <strong>全局锁</strong> 并回滚本地事务释放本地锁，tx1 的分支回滚最终成功。</p> <p>因为整个过程 <strong>全局锁</strong> 在 tx1 结束前一直是被 tx1 持有的，所以不会发生 <strong>脏写</strong> 的问题。</p> <h5 id="读隔离"><a href="#读隔离" class="header-anchor">#</a> 读隔离</h5> <p>在数据库本地事务隔离级别 <strong>读已提交（Read Committed）</strong> 或以上的基础上，Seata（AT 模式）的默认全局隔离级别是 <strong>读未提交（Read Uncommitted）</strong> 。</p> <p>如果应用在特定场景下，必需要求全局的 <strong>读已提交</strong> ，目前 Seata 的方式是通过 SELECT FOR UPDATE 语句的代理。</p> <p><img src="../../../img.alicdn.com/tfs/TB138wuwYj1gK0jSZFuXXcrHpXa-724-521.png" alt="Read Isolation: SELECT FOR UPDATE"></p> <p>SELECT FOR UPDATE 语句的执行会申请 <strong>全局锁</strong> ，如果 <strong>全局锁</strong> 被其他事务持有，则释放本地锁（回滚 SELECT FOR UPDATE 语句的本地执行）并重试。这个过程中，查询是被 block 住的，直到 <strong>全局锁</strong> 拿到，即读取的相关数据是 <strong>已提交</strong> 的，才返回。</p> <p>出于总体性能上的考虑，Seata 目前的方案并没有对所有 SELECT 语句都进行代理，仅针对 FOR UPDATE 的 SELECT 语句。</p> <h5 id="工作机制"><a href="#工作机制" class="header-anchor">#</a> 工作机制</h5> <p>以一个示例来说明整个 AT 分支的工作过程。</p> <p>业务表：<code>product</code></p> <table><thead><tr><th>Field</th> <th>Type</th> <th>Key</th></tr></thead> <tbody><tr><td>id</td> <td>bigint(20)</td> <td>PRI</td></tr> <tr><td>name</td> <td>varchar(100)</td> <td></td></tr> <tr><td>since</td> <td>varchar(100)</td> <td></td></tr></tbody></table> <p>AT 分支事务的业务逻辑：</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">update</span> product <span class="token keyword">set</span> name <span class="token operator">=</span> <span class="token string">'GTS'</span> <span class="token keyword">where</span> name <span class="token operator">=</span> <span class="token string">'TXC'</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h6 id="一阶段"><a href="#一阶段" class="header-anchor">#</a> 一阶段</h6> <p>过程：</p> <ol><li>解析 SQL：得到 SQL 的类型（UPDATE），表（product），条件（where name = 'TXC'）等相关的信息。</li> <li>查询更新前数据的镜像（下面称：前镜像）：根据解析得到的条件信息，生成查询语句，定位数据。</li></ol> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">select</span> id<span class="token punctuation">,</span> name<span class="token punctuation">,</span> since <span class="token keyword">from</span> product <span class="token keyword">where</span> name <span class="token operator">=</span> <span class="token string">'TXC'</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>得到前镜像：</p> <table><thead><tr><th>id</th> <th>name</th> <th>since</th></tr></thead> <tbody><tr><td>1</td> <td>TXC</td> <td>2014</td></tr></tbody></table> <ol><li>执行业务 SQL：更新这条记录的 name 为 'GTS'。</li> <li>查询更新后数据的镜像（下面称：后镜像）：根据前镜像的结果，通过 <strong>主键</strong> 定位数据。</li></ol> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">select</span> id<span class="token punctuation">,</span> name<span class="token punctuation">,</span> since <span class="token keyword">from</span> product <span class="token keyword">where</span> id <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>得到后镜像：</p> <table><thead><tr><th>id</th> <th>name</th> <th>since</th></tr></thead> <tbody><tr><td>1</td> <td>GTS</td> <td>2014</td></tr></tbody></table> <ol><li>插入回滚日志：把前后镜像数据以及业务 SQL 相关的信息组成一条回滚日志记录，插入到 <code>UNDO_LOG</code> 表中。</li></ol> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
	<span class="token property">&quot;branchId&quot;</span><span class="token operator">:</span> <span class="token number">641789253</span><span class="token punctuation">,</span>
	<span class="token property">&quot;undoItems&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
		<span class="token property">&quot;afterImage&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
			<span class="token property">&quot;rows&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
				<span class="token property">&quot;fields&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
					<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">,</span>
					<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
					<span class="token property">&quot;value&quot;</span><span class="token operator">:</span> <span class="token number">1</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
					<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span>
					<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
					<span class="token property">&quot;value&quot;</span><span class="token operator">:</span> <span class="token string">&quot;GTS&quot;</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
					<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;since&quot;</span><span class="token punctuation">,</span>
					<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
					<span class="token property">&quot;value&quot;</span><span class="token operator">:</span> <span class="token string">&quot;2014&quot;</span>
				<span class="token punctuation">}</span><span class="token punctuation">]</span>
			<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
			<span class="token property">&quot;tableName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;product&quot;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token property">&quot;beforeImage&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
			<span class="token property">&quot;rows&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
				<span class="token property">&quot;fields&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
					<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">,</span>
					<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
					<span class="token property">&quot;value&quot;</span><span class="token operator">:</span> <span class="token number">1</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
					<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span>
					<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
					<span class="token property">&quot;value&quot;</span><span class="token operator">:</span> <span class="token string">&quot;TXC&quot;</span>
				<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
					<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;since&quot;</span><span class="token punctuation">,</span>
					<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
					<span class="token property">&quot;value&quot;</span><span class="token operator">:</span> <span class="token string">&quot;2014&quot;</span>
				<span class="token punctuation">}</span><span class="token punctuation">]</span>
			<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
			<span class="token property">&quot;tableName&quot;</span><span class="token operator">:</span> <span class="token string">&quot;product&quot;</span>
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token property">&quot;sqlType&quot;</span><span class="token operator">:</span> <span class="token string">&quot;UPDATE&quot;</span>
	<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
	<span class="token property">&quot;xid&quot;</span><span class="token operator">:</span> <span class="token string">&quot;xid:xxx&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br></div></div><ol><li>提交前，向 TC 注册分支：申请 <code>product</code> 表中，主键值等于 1 的记录的 <strong>全局锁</strong> 。</li> <li>本地事务提交：业务数据的更新和前面步骤中生成的 UNDO LOG 一并提交。</li> <li>将本地事务提交的结果上报给 TC。</li></ol> <h6 id="二阶段-回滚"><a href="#二阶段-回滚" class="header-anchor">#</a> 二阶段-回滚</h6> <ol><li>收到 TC 的分支回滚请求，开启一个本地事务，执行如下操作。</li> <li>通过 XID 和 Branch ID 查找到相应的 UNDO LOG 记录。</li> <li>数据校验：拿 UNDO LOG 中的后镜与当前数据进行比较，如果有不同，说明数据被当前全局事务之外的动作做了修改。这种情况，需要根据配置策略来做处理，详细的说明在另外的文档中介绍。</li> <li>根据 UNDO LOG 中的前镜像和业务 SQL 的相关信息生成并执行回滚的语句：</li></ol> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">update</span> product <span class="token keyword">set</span> name <span class="token operator">=</span> <span class="token string">'TXC'</span> <span class="token keyword">where</span> id <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ol><li>提交本地事务。并把本地事务的执行结果（即分支事务回滚的结果）上报给 TC。</li></ol> <h6 id="二阶段-提交"><a href="#二阶段-提交" class="header-anchor">#</a> 二阶段-提交</h6> <ol><li>收到 TC 的分支提交请求，把请求放入一个异步任务的队列中，马上返回提交成功的结果给 TC。</li> <li>异步任务阶段的分支提交请求将异步和批量地删除相应 UNDO LOG 记录。</li></ol> <h5 id="附录"><a href="#附录" class="header-anchor">#</a> 附录</h5> <h6 id="回滚日志表"><a href="#回滚日志表" class="header-anchor">#</a> 回滚日志表</h6> <p>UNDO_LOG Table：不同数据库在类型上会略有差别。</p> <p>以 MySQL 为例：</p> <table><thead><tr><th>Field</th> <th>Type</th></tr></thead> <tbody><tr><td>branch_id</td> <td>bigint PK</td></tr> <tr><td>xid</td> <td>varchar(100)</td></tr> <tr><td>context</td> <td>varchar(128)</td></tr> <tr><td>rollback_info</td> <td>longblob</td></tr> <tr><td>log_status</td> <td>tinyint</td></tr> <tr><td>log_created</td> <td>datetime</td></tr> <tr><td>log_modified</td> <td>datetime</td></tr></tbody></table> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token comment">-- 注意此处0.7.0+ 增加字段 context</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>undo_log<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>branch_id<span class="token punctuation">`</span> <span class="token keyword">bigint</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>xid<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>context<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>rollback_info<span class="token punctuation">`</span> <span class="token keyword">longblob</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>log_status<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>log_created<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>log_modified<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">UNIQUE</span> <span class="token keyword">KEY</span> <span class="token punctuation">`</span>ux_undo_log<span class="token punctuation">`</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>xid<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>branch_id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h4 id="tcc-模式"><a href="#tcc-模式" class="header-anchor">#</a> TCC 模式</h4> <p>回顾总览中的描述：一个分布式的全局事务，整体是 <strong>两阶段提交</strong> 的模型。全局事务是由若干分支事务组成的，分支事务要满足 <strong>两阶段提交</strong> 的模型要求，即需要每个分支事务都具备自己的：</p> <ul><li>一阶段 prepare 行为</li> <li>二阶段 commit 或 rollback 行为</li></ul> <p><img src="../../../img.alicdn.com/tfs/TB14Kguw1H2gK0jSZJnXXaT1FXa-853-482.png" alt="Overview of a global transaction"></p> <p>根据两阶段行为模式的不同，我们将分支事务划分为 <strong>Automatic (Branch) Transaction Mode</strong> 和 <strong>Manual (Branch) Transaction Mode</strong>.</p> <p>AT 模式（<a href="https://seata.io/zh-cn/docs/overview/what-is-seata.html" target="_blank" rel="noopener noreferrer">参考链接 TBD<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>）基于 <strong>支持本地 ACID 事务</strong> 的 <strong>关系型数据库</strong>：</p> <ul><li>一阶段 prepare 行为：在本地事务中，一并提交业务数据更新和相应回滚日志记录。</li> <li>二阶段 commit 行为：马上成功结束，<strong>自动</strong> 异步批量清理回滚日志。</li> <li>二阶段 rollback 行为：通过回滚日志，<strong>自动</strong> 生成补偿操作，完成数据回滚。</li></ul> <p>相应的，TCC 模式，不依赖于底层数据资源的事务支持：</p> <ul><li>一阶段 prepare 行为：调用 <strong>自定义</strong> 的 prepare 逻辑。</li> <li>二阶段 commit 行为：调用 <strong>自定义</strong> 的 commit 逻辑。</li> <li>二阶段 rollback 行为：调用 <strong>自定义</strong> 的 rollback 逻辑。</li></ul> <p>所谓 TCC 模式，是指支持把 <strong>自定义</strong> 的分支事务纳入到全局事务的管理中。</p> <h2 id="_4、seata-综合案例"><a href="#_4、seata-综合案例" class="header-anchor">#</a> 4、Seata 综合案例</h2> <h3 id="_1准备业务功能"><a href="#_1准备业务功能" class="header-anchor">#</a> ①准备业务功能</h3> <h4 id="_1-解压已有maven工程"><a href="#_1-解压已有maven工程" class="header-anchor">#</a> [1]解压已有Maven工程</h4> <p>工程解压后的目录：Seata案例物料包\code\fescar-parent</p> <h4 id="_2-使用-idea-打开-fescar-parent"><a href="#_2-使用-idea-打开-fescar-parent" class="header-anchor">#</a> [2]使用 IDEA 打开 fescar-parent</h4> <p>请把 fescar-parent 放在一个非中文没有空格的目录</p> <h4 id="_3-启动-nacos-服务器"><a href="#_3-启动-nacos-服务器" class="header-anchor">#</a> [3]启动 Nacos 服务器</h4> <p>Seata案例物料包\tools\nacos\bin&gt;startup.cmd -m standalone</p> <h4 id="_4-在-fescar-parent-的各个模块工程中修改-nacos-地址"><a href="#_4-在-fescar-parent-的各个模块工程中修改-nacos-地址" class="header-anchor">#</a> [4]在 fescar-parent 的各个模块工程中修改 Nacos 地址</h4> <p><img src="" alt="images"></p> <h4 id="_5-数据库建模"><a href="#_5-数据库建模" class="header-anchor">#</a> [5]数据库建模</h4> <p>依次执行下列 SQL 文件：</p> <blockquote><p>Seata案例物料包\dbsql\fescar-business.sql<br></p> <p>Seata案例物料包\dbsql\fescar-item.sql<br></p> <p>Seata案例物料包\dbsql\fescar-order.sql<br></p> <p>Seata案例物料包\dbsql\fescar-user.sql<br></p></blockquote> <h4 id="_6-在-fescar-parent-的各个模块工程中修改数据库地址"><a href="#_6-在-fescar-parent-的各个模块工程中修改数据库地址" class="header-anchor">#</a> [6]在 fescar-parent 的各个模块工程中修改数据库地址</h4> <p><img src="" alt="images"></p> <h4 id="_7-在-user-微服务中有-10-0"><a href="#_7-在-user-微服务中有-10-0" class="header-anchor">#</a> [7]在 user 微服务中有 10 / 0</h4> <p>为了测试分布式事务情况，user 微服务中有 10 / 0 的代码。</p> <p>所在类是：com.atguigu.service.impl.UserInfoServiceImpl</p> <p>所在方法：decrMoney()</p> <p>初步测试的时候先注释掉。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Transactional</span><span class="token punctuation">(</span>rollbackFor <span class="token operator">=</span> <span class="token class-name">Exception</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">decrMoney</span><span class="token punctuation">(</span><span class="token class-name">String</span> username<span class="token punctuation">,</span> <span class="token keyword">int</span> money<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">UserInfo</span> userInfo <span class="token operator">=</span> userInfoMapper<span class="token punctuation">.</span><span class="token function">selectByPrimaryKey</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">;</span>
    userInfo<span class="token punctuation">.</span><span class="token function">setMoney</span><span class="token punctuation">(</span>userInfo<span class="token punctuation">.</span><span class="token function">getMoney</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> count <span class="token operator">=</span> userInfoMapper<span class="token punctuation">.</span><span class="token function">updateByPrimaryKeySelective</span><span class="token punctuation">(</span>userInfo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;添加用户受影响行数：&quot;</span><span class="token operator">+</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// int q=10/0;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_2整合-seata"><a href="#_2整合-seata" class="header-anchor">#</a> ②整合 Seata</h3></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="../assets/js/app.972bd0f0.js" defer></script><script src="../assets/js/4.904e76b5.js" defer></script><script src="../assets/js/8.e2571ee7.js" defer></script>
  </body>

<!-- Mirrored from heavy_code_industry.gitee.io/code_heavy_industry/pro015-Distributed-Transaction/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 10 May 2022 04:34:16 GMT -->
</html>
